<template>
  <HeaderTitle title="创建会议" />
  <el-row>
    <el-col :span="16">
      <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="会议名称" prop="meetingName">
        <el-input v-model="formData.meetingName" clearable placeholder="请输入会议名称" />
      </el-form-item>
      <el-row justify="space-between">
        <el-col :span="21">
          <el-form-item label="参会人员" prop="userIds">
            <el-select v-model="formData.userIds" multiple>
              <el-option v-for="item in userOption" :key="item.id" :label="item.userName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3" style="text-align: end;">
          <el-button type="primary" @click="openUserForm()">选择</el-button>
        </el-col>
      </el-row>
      <el-form-item label=" " style="text-align: center;">
        <el-button :disabled="formLoading" type="primary" @click="submitForm" v-if="formType != '查看'">确 定</el-button>
        <el-button  @click="setClose">取 消</el-button>
      </el-form-item>
      <UserForm ref="userFormRef" @querySelect="querySelect" :oldUserIds="userIds"/>
    </el-form>
    </el-col>
  </el-row>
</template>
<script lang="ts" setup>
import * as MeetingeApi from '@/api/infoArrive/teleMeeting/index'
import UserForm from '@/views/infoArrive/arriveNotice/userGroup/userForm.vue'

defineOptions({ name: 'MessageTemplateForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref<any>({
  id: null,
  meetingName: '',
  userIds: [],
})
const userOption = ref<any>([])

const formRules = reactive({
  meetingName: [{ required: true, message: '会议名称不能为空', trigger: 'blur' }],
  userIds: [{ required: true, message: '请选择', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string) => {
  dialogVisible.value = true
  dialogTitle.value = type
  formType.value = type
  resetForm()
}

/** 选择操作 */
const userFormRef = ref()
const openUserForm = () => {
  userFormRef.value.open()
}
// 添加确认的回调
const querySelect = (value) => {
  userOption.value = value
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success','setClose']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const query = {
      ...formData.value,
      userIds: formData.value.userIds.join(',')
    }
    if (formType.value === '新增') {
      await MeetingeApi.saveMeeting(query)
      message.success(t('common.createSuccess'))
    } else {
      await MeetingeApi.saveMeeting(query)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
    setClose()
  } finally {
    formLoading.value = false
  }
}

/** userIds */
const userIds = ref('')
watch(
  () => userOption.value,
  () => {
    formData.value.userIds = userOption.value.map(item=>item.id)
    userIds.value = formData.value.userIds.join(',')
  }
)
watch(
  () => formData.value.userIds,
  () => {
    userIds.value = formData.value.userIds.join(',')
  }
)
/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: null,
    meetingName: '',
    userIds: [],
  }
  userOption.value = []
  formRef.value?.resetFields()
}
const setClose=()=>{
  dialogVisible.value = false
  emit('setClose')
}
</script>
